
package com.putao.album.child;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.putao.album.R;
import com.putao.album.base.BaseActivity;
import com.putao.album.dialog.DialogBuilderFactory;
import com.putao.album.dialog.ImportDialogBuilder;
import com.putao.album.eventbus.BasePostEvent;
import com.putao.album.util.ActivityHelper;
import com.putao.album.util.CommonUtils;
import com.putao.album.util.HttpRequestUtil;
import com.putao.album.util.Loger;
import com.putao.album.util.PuTaoConstants;
import com.putao.album.util.StringHelper;
import com.putao.album.util.UmengTouchEvent;
import com.putao.album.zbar.CaptureActivity;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import de.greenrobot.event.EventBus;

/**
 * Created by chen on 15-4-27.
 * 关联宝宝
 */
public class ActivityChildConnect extends BaseActivity {
    private TextView title_tv;
    private Button right_btn, connect_ok;
    private EditText invitation_code;
    private String code;
    private String relation;

    @Override
    public int doGetContentViewId() {
        return R.layout.activity_child_connect;
    }

    @Override
    public void doInitSubViews(View view) {
        title_tv = (TextView) findViewById(R.id.title_tv);
        title_tv.setText(R.string.child_detail_connect_titile);
        right_btn = queryViewById(R.id.right_btn, true);
        right_btn.setBackgroundResource(R.drawable.nav_btn_scan);
        invitation_code = (EditText) findViewById(R.id.invitation_code);
        connect_ok = (Button) findViewById(R.id.connect_ok);
        addOnClickListener(R.id.connect_ok, R.id.back_btn);
        EventBus.getDefault().register(this);
    }

    @Override
    public void doInitData() {
    }

    public void onEvent(BasePostEvent event) {
        switch (event.eventCode) {
            case PuTaoConstants.EVENTBUS_SCAN_QR_CODE:
                code = event.bundle.getString("vcode");
                Loger.d("invite code:" + code);
                startConnectBaby();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.connect_ok:
                startConnectBaby();
                break;
            case R.id.right_btn:
                ActivityHelper.startActivity(mActivity, CaptureActivity.class);
                break;
            default:
                break;
        }
    }


    private void startConnectBaby() {
        if (StringHelper.isEmpty(code)) {
            code = invitation_code.getText().toString();
        }
        if (StringHelper.isEmpty(code)) {
            showToast("请输入邀请码");
            return;
        }
        MobclickAgent.onEvent(mContext, UmengTouchEvent.RELATE_BABY);
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("code", code);
        if (!StringHelper.isEmpty(relation)) {
            params.put("relationship", relation);
        }
        HttpRequestUtil.doHttpPostRequest(mContext, PuTaoConstants.API_POST_BABY_CONNECT,
                new RequestParams(HttpRequestUtil.buildHttpRequestParams(params)), true, new HttpRequestUtil.RequestResponseListener() {
                    @Override
                    public void success(int statuscode, String response) {
                        try {
                            JSONObject json = new JSONObject(response);
                            String status = json.getString("status");
                            String msg = json.getString("msg");
                            if (status.equals(HttpRequestUtil.ResponseCode.api_code_200)) {
                                //{ "msg": "加入成功", "data": {"baby_id": "1321"},"status": 200}
                                String data = json.getString("data");
                                json = new JSONObject(data);
                                String baby_id = json.getString("baby_id");
                                CommonUtils.saveBabyId(baby_id);

                                Bundle bundle = new Bundle();
                                EventBus.getDefault().post(new BasePostEvent(PuTaoConstants.EVENTBUS_RELATE_BABY_SUCCESS, bundle));
                                showToast(msg);
                                finish();
                            } else if (status.equals(HttpRequestUtil.ResponseCode.api_code_10003)) {
                                //需要关系人信息
                                showImportDialog();
                            } else {
                                showToast(msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void failed(int statuscode, String response) {
                        Loger.d("response:::" + response);
                    }
                });
    }

    void showImportDialog() {
        new DialogBuilderFactory<ImportDialogBuilder>(mContext, DialogBuilderFactory.DialogType.IMPORT).Builder()
                .setDialogButtonOnClickListener(mDialogListener).setMessage(getString(R.string.child_connect_input_relation))
                .setNegativeButton(R.string.btn_ok).setPositiveButton(R.string.btn_cancle).Create().show();
    }

    ImportDialogBuilder.DialogButtonOnClickListener mDialogListener = new ImportDialogBuilder.DialogButtonOnClickListener() {
        @Override
        public void positiveOnClick(Dialog dialog, String importText) {
            dialog.dismiss();
        }

        @Override
        public void negativeOnClick(Dialog dialog, String importText) {
            dialog.dismiss();
            if (!StringHelper.isEmpty(importText)) {
                relation = importText;
                startConnectBaby();
            }
        }
    };
}
